var form = layui.form
    ,id=getQueryVariable("id")
    ,code="";

//表单初始值，修改时调用
/*if(id!=""){
    form.val("component-form",{
        "companyname":"公司名称",
        "companyid":"公司编码",
        "note":"备注"
    })
    $("input[name='companyid']").attr('readonly',true)
}*/

queryByid(id);
if(id=="") {

    $("span")[0].innerHTML="新增公司";
   // $("#companylist")
  //  $("#companylist")[0].attr("readOnly",true);
}else{
    //$("input[name='companyid']").attr('readonly',true);
    $("#companyid")[0].readOnly=true;
    //$("#companylist")[0].remove("readOnly");
    $("span")[0].innerHTML="修改组织机构";
}

//监听提交
form.on('submit(component-form-element)', function(data){
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    var num=0;
    if(id==""){
       num= insert(data);
    }else{
       num= update(data);
    }
    if(num <10000 && num > 0){
        layer.msg("保存成功!", {icon: 6});
        setTimeout(function () {
            //保存成功则刷新提交过来的界面 并关闭此页面
            parent.detab("views/jsp/SystemCenter/organization.jsp");
            parent.intab("views/jsp/SystemCenter/organization.jsp","组织结构");

            parent.detab("views/jsp/SystemCenter/tpl/newupcompany.jsp?id="+id)
            parent.detab("views/jsp/SystemCenter/tpl/newupcompany.jsp","新增公司");

        },1000)
    }else if(num <10000 && num ==0){
        layer.msg("保存失败!", {icon: 6});
    }else if (num >10000){
        layer.msg("同步云枢保存失败!", {icon: 6});
    }



    //关闭窗口
   // parent.layui.admin.events.closeThisTabs();//关闭当前tab页
    return false;
});
//设置公司主信息
$('#companylist').click(function () {
    if(id==""){
        layer.msg("请先保存后再添加主数据");
        return false;
    }
    parent.intab("views/jsp/SystemCenter/tpl/newupcompanylist.jsp"+(code==''?'':'?pid='+code+"&id="+id),"公司主信息");
    parent.detab("views/jsp/SystemCenter/tpl/newupcompany.jsp?id="+id)
    parent.detab("views/jsp/SystemCenter/tpl/newupcompany.jsp","新增公司");
   // parent.detab("views/jsp/SystemCenter/tpl/newupcompany.jsp"+(pid==''?'':'?pid='+pid));
 //   parent.detab("views/jsp/SystemCenter/tpl/newupcompany.jsp");
   // parent.detab("views/jsp/SystemCenter/tpl/newupcompany.jsp?id="+id)
});
function queryByid(id) {
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:{"id":id},
        url:'department/selectByKey',
       dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
               /* form.val("component-form",{
                    "companyname":da.data.name,
                    "companyid":da.data.code,
                    "note":da.data.desc
                })*/
                $("#companyname").val(da.data.name);
                $("#companyid").val(da.data.code);
                $("#desc").val(da.data.desc);
                code=da.data.code;
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
}
//新增公司
function insert(data) {
    var tf=false;
    data.field.companyCode=data.field.code;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data.field),
        url:'department/insert',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                //tf=true;
                tf=da.data;

            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
    return tf;
}
function  update(data) {
    var tf=false;
    data.field.id=id;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data.field),
        url:'department/update',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                //tf=true;
                tf=da.data;
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
    return tf;
}